#include "pch.h"
#include "jlink.h"

void  (WINAPI *JLINK_SetLogFile)(char *file);
DWORD(WINAPI *JLINK_GetDLLVersion)(void);
DWORD(WINAPI *JLINK_GetHardwareVersion)(void);
DWORD(WINAPI *JLINK_GetFirmwareString)(char *buff, int count);
DWORD(WINAPI *JLINK_GetSN)(void);
BOOL(WINAPI *JLINK_ExecCommand)(char* cmd, int a, int b);
DWORD(WINAPI *JLINK_TIF_Select)(int type);
void  (WINAPI *JLINK_SetSpeed)(int speed);
DWORD(WINAPI *JLINK_GetSpeed)(void);
DWORD(WINAPI *JLINK_GetId)(void);
int(*JLINK_DEVICE_GetIndex)(const char* sDeviceName);
int(*JLINK_DEVICE_GetInfo)(int DeviceIndex, JLINKARM_DEVICE_INFO* pDeviceInfo);
int(*JLINK_DEVICE_SelectDialog)(void* hParent, DWORD Flags, JLINKARM_DEVICE_SELECT_INFO* pInfo);
BOOL(WINAPI *JLINK_Open)(void);
void  (WINAPI *JLINK_Close)(void);
BOOL(WINAPI *JLINK_IsOpen)(void);
BOOL(WINAPI *JLINK_Connect)(void);
BOOL(WINAPI *JLINK_IsConnected)(void);
int   (WINAPI *JLINK_Halt)(void);
BOOL(WINAPI *JLINK_IsHalted)(void);
void  (WINAPI *JLINK_SetResetType)(int type);
void  (WINAPI *JLINK_Reset)(void);
void  (WINAPI *JLINK_Go)(void);
void  (WINAPI *JLINK_GoIntDis)(void);
DWORD(WINAPI *JLINK_ReadReg)(int index);
int   (WINAPI *JLINK_WriteReg)(int index, DWORD data);
int   (WINAPI *JLINK_ReadMem)(DWORD addr, int len, void *buf);
int   (WINAPI *JLINK_WriteMem)(DWORD addr, int len, void *buf);
int   (WINAPI *JLINK_WriteU8)(DWORD addr, BYTE data);
int   (WINAPI *JLINK_WriteU16)(DWORD addr, WORD data);
int   (WINAPI *JLINK_WriteU32)(DWORD addr, DWORD data);
int   (WINAPI *JLINK_EraseChip)(void);
int   (WINAPI *JLINK_DownloadFile)(LPCSTR file, DWORD addr);
void  (WINAPI *JLINK_BeginDownload)(int index);
void  (WINAPI *JLINK_EndDownload)(void);

BOOL JLINK_Init(void)
{
	HMODULE hModule = LoadLibraryExA("JLinkARM.dll", NULL, 0);
	if (hModule == NULL)
	{
		return FALSE;
	}
	(FARPROC &)JLINK_GetDLLVersion       = GetProcAddress(hModule, "JLINK_GetDLLVersion");
	(FARPROC &)JLINK_GetHardwareVersion  = GetProcAddress(hModule, "JLINK_GetHardwareVersion");
	(FARPROC &)JLINK_GetFirmwareString   = GetProcAddress(hModule, "JLINK_GetFirmwareString");
	(FARPROC &)JLINK_GetSN               = GetProcAddress(hModule, "JLINK_GetSN");
	(FARPROC &)JLINK_GetId               = GetProcAddress(hModule, "JLINK_GetId");
	(FARPROC &)JLINK_SetLogFile          = GetProcAddress(hModule, "JLINK_SetLogFile");
	(FARPROC &)JLINK_ExecCommand         = GetProcAddress(hModule, "JLINK_ExecCommand");
	(FARPROC &)JLINK_DEVICE_GetIndex     = GetProcAddress(hModule, "JLINKARM_DEVICE_GetIndex");
	(FARPROC &)JLINK_DEVICE_GetInfo      = GetProcAddress(hModule, "JLINKARM_DEVICE_GetInfo");
	(FARPROC &)JLINK_DEVICE_SelectDialog = GetProcAddress(hModule, "JLINKARM_DEVICE_SelectDialog");
	(FARPROC &)JLINK_SetSpeed            = GetProcAddress(hModule, "JLINK_SetSpeed");
	(FARPROC &)JLINK_GetSpeed            = GetProcAddress(hModule, "JLINK_GetSpeed");
	(FARPROC &)JLINK_TIF_Select          = GetProcAddress(hModule, "JLINK_TIF_Select");
	(FARPROC &)JLINK_Open                = GetProcAddress(hModule, "JLINK_Open");
	(FARPROC &)JLINK_Close               = GetProcAddress(hModule, "JLINK_Close");
	(FARPROC &)JLINK_IsOpen              = GetProcAddress(hModule, "JLINK_IsOpen");
	(FARPROC &)JLINK_Connect             = GetProcAddress(hModule, "JLINK_Connect");
	(FARPROC &)JLINK_IsConnected         = GetProcAddress(hModule, "JLINK_IsConnected");
	(FARPROC &)JLINK_SetResetType        = GetProcAddress(hModule, "JLINK_SetResetType");
	(FARPROC &)JLINK_Reset               = GetProcAddress(hModule, "JLINK_Reset");
	(FARPROC &)JLINK_Halt                = GetProcAddress(hModule, "JLINK_Halt");
	(FARPROC &)JLINK_IsHalted            = GetProcAddress(hModule, "JLINK_IsHalted");
	(FARPROC &)JLINK_Go                  = GetProcAddress(hModule, "JLINK_Go");
	(FARPROC &)JLINK_GoIntDis            = GetProcAddress(hModule, "JLINK_GoIntDis");
	(FARPROC &)JLINK_ReadReg             = GetProcAddress(hModule, "JLINK_ReadReg");
	(FARPROC &)JLINK_WriteReg            = GetProcAddress(hModule, "JLINK_WriteReg");
	(FARPROC &)JLINK_ReadMem             = GetProcAddress(hModule, "JLINK_ReadMem");
	(FARPROC &)JLINK_WriteMem            = GetProcAddress(hModule, "JLINK_WriteMem");
	(FARPROC &)JLINK_WriteU8             = GetProcAddress(hModule, "JLINK_WriteU8");
	(FARPROC &)JLINK_WriteU16            = GetProcAddress(hModule, "JLINK_WriteU16");
	(FARPROC &)JLINK_WriteU32            = GetProcAddress(hModule, "JLINK_WriteU32");
	(FARPROC &)JLINK_EraseChip           = GetProcAddress(hModule, "JLINK_EraseChip");
	(FARPROC &)JLINK_DownloadFile        = GetProcAddress(hModule, "JLINK_DownloadFile");
	(FARPROC &)JLINK_BeginDownload       = GetProcAddress(hModule, "JLINK_BeginDownload");
	(FARPROC &)JLINK_EndDownload         = GetProcAddress(hModule, "JLINK_EndDownload");

	return TRUE;
}
